@isTest

private class AASTestIDCClass{

/* test class for IDC AAS classes/triggers */

private static user createUser(){
profile aas_p = [Select Id from profile q where profile.Name ='Advanced Ad Solutions'];  
UserRole aas_role = [select id from UserRole where userRole.Name = 'AAS Collections '];

User nuser = new User();     
    nuser.UserName = 'aasTestUser@atti-temp.com'; 
    nuser.LastName = 'aasTest NJ';      
    nuser.Email = 'a_test@atti.com';      
    nuser.Alias = 'Aas';      
    nuser.CommunityNickname = 'aasTest';       
    nuser.TimeZoneSidKey = 'America/Los_Angeles'; 
    nuser.LocaleSidKey = 'en_US';    
    nuser.EmailEncodingKey = 'UTF-8';  
    nuser.ProfileId = aas_p.Id;    
    nuser.UserRoleId = aas_role.Id;
    nuser.LanguageLocaleKey = 'en_US';  
    
    insert nuser;
    return nuser;
}

private static account CreateAccount(){
ID accountRecordTypeID = RecordTypeHelper.GetRecordTypeID('Account',  'AAS Sales Account');
    account acc = new account();
    acc.name = 'AAS Test Acc';
    acc.Billing_Contact__c = 'AAS Team';
    acc.Contact_Phone__c = '8889990034';
    acc.Contact_Fax__c = '8889977934';
    acc.Contact_email__c = 'aas@atti.com';
    acc.Payment_Schedule__c = 'Annual Invoice';
    acc.Payment_Method__c = 'cash';
    acc.RecordTypeId = accountRecordTypeID;    
  
    insert acc;
    return acc;
}

private static Opportunity CreateOpportunity(account a, string stage){    
 date myDate = date.today();
 date newDate = myDate.addDays(2);

        Opportunity o = new Opportunity();      
        o.RecordTypeId = RecordTypeHelper.GetRecordTypeId('Opportunity', 'Advanced Ad Solutions');      
        o.Name = 'Unit Test';      
        o.StageName = stage;
        o.Contract_Start_Date__c =Date.today();
        o.Contract_Termination_Date__c = newDate;        
        o.CloseDate = Date.today();
        o.amount= 500;
        o.LeadSource = 'Newspaper';
        //o.Ordered_Amount__c = 100;
        o.Credit_Check_Info_Secured__c = 'Not Sure';
      //  o.account = acc.name;
        insert o;
        Attachment attch= new Attachment(Name ='Testattch', ParentId = o.Id, Body = Blob.valueOf('This is a Test'));
        insert attch;
        return o;
  }
private static OpportunityLineItem  CreateOLI(Opportunity o, string productname){    
    Pricebook2 s = [select id from Pricebook2 where IsStandard = true];    
    // create the product
    Product2 p1 = new Product2(
        name = productname,//'Test Product 1',
        IsActive=true,
        Description='My Product',
        ProductCode='12345'
    );
    insert p1;       
   
    // create the pricebookentry
    PricebookEntry pbe1 = new PricebookEntry(
        Pricebook2Id=s.id,
        Product2Id=p1.id,
        UnitPrice=0.00,
        IsActive=true,
        UseStandardPrice=false
    );
    insert pbe1;   

   OpportunityLineItem oli = new OpportunityLineItem(); 
    oli.Quantity = 1;
    oli.TotalPrice = 1;
    oli.PricebookEntryId = pbe1.id;
    oli.OpportunityId = o.id;    
    insert oli;      
   return oli;  
    }  
  
private static Order__c CreatefullfilmentOrder(Opportunity o){    
   date myDate = date.today();
   date cDate = myDate.addDays(2);
   date rDate = myDate.addDays(4);

   Order__c fo = new Order__c();      
   fo.Opportunity__c = o.id;     
   fo.Campaign_End_Date__c = cDate;
   fo.Renewal_Date__c = rDate;
   fo.Fulfillment_Status__c = 'yes';
   fo.Ordered_Product_Amount__c = 500;
   fo.Credit_Check_Info_Secured__c ='yes';
   
   insert fo;      
   return fo;  
    }

static testMethod void testLockProduct() 
{
   Test.startTest();   //change by naidu.
   account act = CreateAccount();
    opportunity testoppty = CreateOpportunity(act, 'Ready For Order');
    OpportunityLineItem  OptyLineitem = CreateOLI(testoppty,'Test Product 1'); 
    OpportunityLineItem  OptyLineitem1 = CreateOLI(testoppty,'Test Product 2');   
    testoppty.StageName = 'Signed';
    try{update  testoppty; }
    catch(exception e){system.debug('Error:'+e);}
    try{Delete OptyLineitem1; }
    catch(exception e){system.debug('Error:'+e);}
    Test.stopTest();
    
}

static testMethod void testInitiateOrders() 
{
    Test.startTest();//change by naidu.
   account act = CreateAccount();
    opportunity testoppty = CreateOpportunity(act, 'Ready For Order');
    OpportunityLineItem  OptyLineitem = CreateOLI(testoppty,'Test Product 1'); 
    OpportunityLineItem  OptyLineitem1 = CreateOLI(testoppty,'Test Product 2');   
    testoppty.StageName = 'Signed';
    try{update  testoppty; }
    catch(exception e){system.debug('Error:'+e);}
    InitiateFulfillmentOrder.CreateFulfillmentOrders(string.valueof(testoppty.id));    
    Test.stopTest();
}


static testMethod void testDeleteOrders() 
{
Test.startTest();   //change by naidu.
   account act = CreateAccount();
    opportunity testoppty = CreateOpportunity(act, 'Ready For Order');
    OpportunityLineItem  OptyLineitem = CreateOLI(testoppty,'Test Product 1'); 
    OpportunityLineItem  OptyLineitem1 = CreateOLI(testoppty,'Test Product 2');
    testoppty.stageName = 'Signed';  
    testoppty.probability = 90.00; 
    try{update  testoppty; }
    catch(exception e){system.debug('Error:'+e);}    
    testoppty.Order_Initiated__c = true;
    try{update  testoppty; }
    catch(exception e){system.debug('Error:'+e);}  
    
    testoppty.stageName = 'Ready For Order';
    testoppty.probability = 75.00;
    try{update  testoppty; }
    catch(exception e){system.debug('Error:'+e);}
    testoppty.stageName = 'Signed'; 
    testoppty.probability = 90.00;
    testoppty.Order_Initiated__c = true;
    try{update  testoppty; }
    catch(exception e){system.debug('Error:'+e);}
    Test.stopTest();
    }
    
 static testMethod void testValidateOrders()
 {
    Test.startTest();//change by naidu.
    account act = CreateAccount();
    opportunity testoppty2 = CreateOpportunity(act, 'Ready For Order');
    OpportunityLineItem  OptyLineitem2 = CreateOLI(testoppty2,'Test Product 1'); 
    OpportunityLineItem  OptyLineitem3 = CreateOLI(testoppty2,'Test Product 2');
    testoppty2.stageName = 'Signed';  
    testoppty2.probability = 90.00; 
    try{Update testoppty2; }
    catch(exception e){system.debug('Error:'+e);}
    Order__c o1 = CreatefullfilmentOrder(testoppty2);
    o1.Creative_Dev_Status__c = 'Creative Approved'; 
    o1.Fulfillment_Status__c = 'Fulfillment Completed';
    o1.Credit_Check_Info_Secured__c = 'Yes';
    Order__c o2 = CreatefullfilmentOrder(testoppty2); 
    o2.Fulfillment_Status__c = 'Approved in Progress';
    Update o1; Update o2;
    testoppty2.StageName = 'Fulfilled/Live';
    testoppty2.probability = 100.00;
    try{update testoppty2;}
    catch(exception e){system.debug(e);}
    Test.stopTest();
}
    
static testMethod void testAASfulfillmentOrders() 
{  
 // user aasTestUser = createUser();    
Test.startTest();//change by naidu.
 date myDate = date.today();
  date newDate = myDate.addDays(2);
  ID lAOpportunityRecordTypeID = RecordTypeHelper.GetRecordTypeID('Opportunity', 'Advanced Ad Solutions');
  ID taskRecordTypeID = RecordTypeHelper.GetRecordTypeID('Task', 'Advanced Ad Solutions');
    List<Order__c>  orderLIst = new List<Order__c>();
    List<Opportunity> OptyList = new list<Opportunity>();        
    List<User> UserList = new List<User>();
    UserList = [select id from user where IsActive = true and profile.Name ='Advanced Ad Solutions' limit 1 ];
    account act = CreateAccount();
    opportunity oppty = CreateOpportunity(act, 'Signed');
   
    oppty.ownerid = userList[0].id;
    
    update oppty;
    
    Order__c fulfillorder = CreatefullfilmentOrder(oppty);
    Order__c fulfillorder2 = CreatefullfilmentOrder(oppty);
    Order__c fulfillorder3 = CreatefullfilmentOrder(oppty);

    fulfillorder.Credit_Check_Info_Secured__c ='yes';
    fulfillorder.Fulfillment_Status__c='Fulfillment Denied';
    fulfillorder.Specify_if_Fulfillment_Denied__c = 'Not enough budget';
    fulfillorder.Contracted_Product_Amount__c=1500;
    
    fulfillorder2.Credit_Check_Info_Secured__c ='no';
    fulfillorder2.renewal_days__c=15;
    
    orderLIst.add(fulfillorder);
    orderLIst.add(fulfillorder2);
    
    update orderLIst;    
    orderLIst.clear();
    
    fulfillorder.Specify_if_Fulfillment_Denied__c='';
    fulfillorder.Fulfillment_Status__c='For AM Review';

    orderLIst.add(fulfillorder);

    
    fulfillorder3.Product__c = 'IYP Banner';
    fulfillorder3.Renewal_Date__c = system.today();

    orderLIst.add(fulfillorder3);
       
    update orderLIst;
    
    task tk = new task();
    tk.recordtypeID = taskRecordTypeID;
    tk.Whatid =fulfillorder3.id;
    tk.Ownerid = oppty.ownerid;
    tk.Priority = 'Normal';
    tk.Status = 'In Progress';
    tk.Subject = 'Meeting';
    tk.ownerid =userList[0].id;
    
    insert tk;
    Test.stopTest();
           
}

}